/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.core.test;

import com.playground.core.entity.GameTitle;
import com.playground.core.entity.Player;
import com.playground.core.entity.Score;
import com.playground.core.repository.GameTitleRepository;
import com.playground.core.repository.PlayerRepository;
import com.playground.core.repository.ScoreRepository;
import java.util.Date;
import org.springframework.context.ApplicationContext;


/**
 *
 * @author Tiago Peres
 */
public class Dataset {
    private GameTitleRepository gameTitleRepository;
    private PlayerRepository playerRepository;
    private ScoreRepository scoreRepository;
    
    // players
    public static Player player1;
    public static Player player2;
    public static Player player3;
    public static Player player4;
    
    // game titles
    public static GameTitle gameTitle1;
    public static GameTitle gameTitle2;
    public static GameTitle gameTitle3;
    
    // scores
    public static Score score1;
    public static Score score2;
    public static Score score3;
    public static Score score4;
    public static Score score5;
    public static Score score6;
    
    public Dataset(ApplicationContext context){
        gameTitleRepository = (GameTitleRepository) context.getBean("gameTitleRepository");
        playerRepository = (PlayerRepository) context.getBean("playerRepository");
        scoreRepository = (ScoreRepository) context.getBean("scoreRepository");
    }
    
    public void loadDatabase(){
        createPlayers();
        createGameTitles();
        createScores();
    }
    
    private void createPlayers(){
        Player p = new Player();
        p.setFacebookId("100002327206645");
        p.setNickname("Tiago");
        player1 = playerRepository.save(p);
        
        p = new Player();
        p.setFacebookId("100003508141437");
        p.setNickname("Cleber");
        player2 = playerRepository.save(p);
        
        p = new Player();
        p.setFacebookId("100001650074191");
        p.setNickname("Felipe");
        player3 = playerRepository.save(p);
        
        p = new Player();
        p.setFacebookId("fb04");
        p.setNickname("Maria");
        player4 = playerRepository.save(p);
    }
    
    private void createGameTitles(){
        GameTitle gt = new GameTitle();
        gt.setLaunchDate(new Date());
        gt.setMaximumNumberOfPlayers(6);
        gt.setName("Monopoly");
        gt.setPlayedTimes(200);
        gt.setRating(5);
        gameTitle1 = gameTitleRepository.save(gt);
        
        gt = new GameTitle();
        gt.setLaunchDate(new Date());
        gt.setMaximumNumberOfPlayers(2);
        gt.setName("Checkers");
        gt.setPlayedTimes(138);
        gt.setRating(4);
        gameTitle2 = gameTitleRepository.save(gt);
        
        gt = new GameTitle();
        gt.setLaunchDate(new Date());
        gt.setMaximumNumberOfPlayers(2);
        gt.setName("Tic-Tac-Toe");
        gt.setPlayedTimes(25);
        gt.setRating(3);
        gameTitle3 = gameTitleRepository.save(gt);
    }
    
    private void createScores(){
        Score s = new Score();
        s.setGameTitle(gameTitle1);
        s.setLosses(20);
        s.setPlayedTimes(30);
        s.setPoints(50);
        s.setVictories(10);
        s.setPlayer(player1);
        score1 = scoreRepository.save(s);
        
        s = new Score();
        s.setGameTitle(gameTitle1);
        s.setLosses(10);
        s.setPlayedTimes(50);
        s.setPoints(100);
        s.setVictories(15);
        s.setPlayer(player2);
        score2 = scoreRepository.save(s);
        
        s = new Score();
        s.setGameTitle(gameTitle2);
        s.setLosses(3);
        s.setPlayedTimes(10);
        s.setPoints(23);
        s.setVictories(4);
        s.setPlayer(player3);
        score3 = scoreRepository.save(s);
        
        s = new Score();
        s.setGameTitle(gameTitle2);
        s.setLosses(15);
        s.setPlayedTimes(32);
        s.setPoints(53);
        s.setVictories(10);
        s.setPlayer(player1);
        score4 = scoreRepository.save(s);
        
        s = new Score();
        s.setGameTitle(gameTitle2);
        s.setLosses(1);
        s.setPlayedTimes(3);
        s.setPoints(8);
        s.setVictories(2);
        s.setPlayer(player2);
        score5 = scoreRepository.save(s);
        
        s = new Score();
        s.setGameTitle(gameTitle3);
        s.setLosses(78);
        s.setPlayedTimes(145);
        s.setPoints(201);
        s.setVictories(60);
        s.setPlayer(player4);
        score5 = scoreRepository.save(s);
    }
    
}
